import Vue from "vue";
import Vuex from "vuex";
import { checkLoginNameAndPassword } from '@/api/employee';


Vue.use(Vuex);

//创建store对象
const store = new Vuex.Store({
  state: {
    // 存放的键值对就是所要管理的状态
    name:'张三',
    user:{
        name:'李四'
    },
    userList:[
        {name:'张三',age:79},
        {name:'李四',age:123},
        {name:'王五',age:12},
    ],
    isLoginnedIn:false,
    userMessage:{},
  },
  mutations: {
    // 定义方法修改state中的数据
    updateName(state,newName){
        state.name = newName;
    },
    updateUserName(state,user){
        state.user = user;
    },
    addUser(state,user){
        state.userList.push(user)
    },
    updateIsLoginnedIn(state,isLoginnedIn){
        state.isLoginnedIn = isLoginnedIn;
        localStorage.setItem('isLoginnedIn',isLoginnedIn)
    },
    updateUserMessage(state,userMessage){
        state.userMessage = userMessage;
        localStorage.setItem('userMessage',JSON.stringify(userMessage))
    }
  },
  actions: {
    async  login({commit},user){
        let result = false;
        await  checkLoginNameAndPassword(user).then((data)=>{
            console.log('login',data);
            if(data.data.code == 200){
                commit('updateIsLoginnedIn',true);
                commit('updateUserMessage',data.data.data);
                result= true;
            }
        })
        return result;
    }
  }
})

export default store;
